{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Requirements\n",
    "!pip install nibabel vtk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import math\n",
    "import numpy as np\n",
    "from k3d.helpers import download"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import vtk\n",
    "from vtk.util import numpy_support\n",
    "\n",
    "filename = download('https://vedo.embl.es/examples/data/embryo.slc')\n",
    "reader = vtk.vtkSLCReader()\n",
    "reader.SetFileName(filename)\n",
    "reader.Update()\n",
    "vti = reader.GetOutput()\n",
    "\n",
    "bounds = vti.GetBounds()\n",
    "x, y, z = vti.GetDimensions()\n",
    "volume_data = numpy_support.vtk_to_numpy(vti.GetPointData().GetArray(0)).reshape(-1, y, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embryo = k3d.volume(volume_data.astype(np.float16), \n",
    "                    color_map=np.array(k3d.basic_color_maps.BlackBodyRadiation, dtype=np.float32), \n",
    "                    bounds=bounds)\n",
    "\n",
    "plot = k3d.plot()\n",
    "plot += embryo\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embryo = k3d.mip(volume_data.astype(np.float16), \n",
    "                 color_map=np.array(k3d.basic_color_maps.BlackBodyRadiation, dtype=np.float32), \n",
    "                 bounds=bounds)\n",
    "\n",
    "plot = k3d.plot(background_color=0, grid_visible=False)\n",
    "plot += embryo\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.lighting = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nibabel as nib\n",
    "\n",
    "filename = download('https://github.com/FNNDSC/data/raw/master/nifti/adi_brain/adi_brain.nii.gz')\n",
    "\n",
    "nii_source = nib.load(filename)\n",
    "img = nii_source.get_fdata()\n",
    "dx, dy, dz = nii_source.header.get_zooms()\n",
    "img = np.swapaxes(img,0,2).astype(np.float32)\n",
    "nz, ny, nx = img.shape\n",
    "\n",
    "volume = k3d.volume(img, color_range=[50,1000], color_map=np.array(k3d.basic_color_maps.Jet, dtype=np.float32))\n",
    "\n",
    "plot = k3d.plot()\n",
    "plot += volume\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "volume.samples = 1024.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "volume.color_range = [650, 1500]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  },
  "nbTranslate": {
   "displayLangs": [
    "en",
    "pl"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "pl",
   "targetLang": "en",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
